/**
 * 
 */
package org.vsg.stock.basic.service;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import org.vsg.common.biz.exception.ServiceException;
import org.vsg.stock.basic.StockHelper;
import org.vsg.stock.basic.domain.StockDayIndicator;
import org.vsg.stock.basic.persistence.StockDailyLineDao;
import org.vsg.stock.basic.persistence.StockDayIndicatorDao;
import org.vsg.stock.basic.service.handler.AnalysisTickHandler;
import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.domain.StockTick;
import org.vsg.stock.core.persistence.StockInfoDao;
import org.vsg.stock.core.persistence.StockTickDao;

/**
 * @author vison
 *
 */

public class StockDayIndicatorServiceImpl implements StockDayIndicatorService , InitializingBean{
	
	@Resource
	private StockInfoDao stockInfoDao;
	
	@Resource
	private StockDailyLineDao stockDailyLineDao;

	private DecimalFormat decFormat = new DecimalFormat("##0.000");	
	
	@Resource
	private StockTickDao stockTickDao;

	@Resource
	private StockDayIndicatorDao stockDayIndicatorDao;
	
	private StockHelper inst = null;
	
	@Resource
	private AnalysisTickHandler analysisTickHandler;
	
	
	@Override
	public void calculate(Date fromDate, Date toDate) {
		// TODO Auto-generated method stub


		
	}


	@Override
	public void genBasicStockDayIndicator(Date refDate , int cycle) throws ServiceException {
		// TODO Auto-generated method stub
		List<String> allCodes = stockInfoDao.findAllCode();
		
		String code = null;
		StockDailyKLine line = null;
		for (Iterator<String> codesIter = allCodes.iterator(); codesIter.hasNext() ; ) {
			code = codesIter.next();
			line = stockDailyLineDao.getStkDailyLineByMinVolInCycle(code, refDate, cycle);
			
			// --- get the another stock line ---
			
			if (line != null) {
				System.out.println(line.getId().getDate() + " , " + code);
			} else {
				System.out.println(code);
			}

		}
		


	}


	@Override
	public void genBasicStockDayIndicator(String stockCode, Date refDate,
			int cycle) throws ServiceException {
		// TODO Auto-generated method stub
		StockDailyKLine line = stockDailyLineDao.getStkDailyLineByMinVolInCycle(stockCode, refDate, cycle);
		// --- get the before date --

		Date tmpDate = null;
		Calendar tmpCale = Calendar.getInstance();
		tmpCale.setTime(line.getId().getDate());
		
		inst = StockHelper.getInstance();

		int beforeCycle = 1;
		while (beforeCycle > 0) {
			tmpCale.add(Calendar.DATE, -1);	
			if (!inst.isTradingDate(tmpCale)) {
				continue;
			}
			--beforeCycle;
		}
		
		Date beforeDate = tmpCale.getTime();

		tmpCale.setTime(line.getId().getDate());
		int afterCycle = 1;
		while (afterCycle > 0) {
			tmpCale.add(Calendar.DATE, 1);	
			if (!inst.isTradingDate(tmpCale)) {
				continue;
			}
			--afterCycle;
		}
		Date afterDate = tmpCale.getTime();
		
		// --- get the vol and tick ---
		List<StockTick> stockTickList = stockTickDao.findOneDayAllTicks(stockCode, beforeDate);
		StockTick stkTick = stockTickList.get(stockTickList.size() - 1);
		
		long firstVol = stkTick.getSumTick();
		int firstTotalTicks = stockTickList.size();

		stockTickList = stockTickDao.findOneDayAllTicks(stockCode, line.getId().getDate());
		stkTick = stockTickList.get(stockTickList.size() - 1);
		
		long secondVol = stkTick.getSumTick();
		int secondTotalTicks = stockTickList.size();

		stockTickList = stockTickDao.findOneDayAllTicks(stockCode, afterDate);
		stkTick = stockTickList.get(stockTickList.size() - 1);
		
		long thirdVol = stkTick.getSumTick();
		int thirdTotalTicks = stockTickList.size();
		
		
		double firstAvgTick = Double.parseDouble( decFormat.format( (double)firstVol / firstTotalTicks ) );
		double secondAvgTick = Double.parseDouble( decFormat.format( (double)secondVol / secondTotalTicks ) );
		double thirdAvgTick = Double.parseDouble( decFormat.format( (double)thirdVol / thirdTotalTicks ) );
		
		double avgTick = Double.parseDouble( decFormat.format( (firstAvgTick + secondAvgTick + thirdAvgTick) / 3 ) ); 

		// --- get the stock ---
		StockDayIndicator indicator = stockDayIndicatorDao.getDayIndicator(stockCode, refDate);
		
		if (indicator == null) {
			indicator = new StockDayIndicator();
			indicator.setStockCode( stockCode );
			indicator.setStockDate( refDate );
			indicator.setAverageTick( avgTick );
			stockDayIndicatorDao.save( indicator );
		} else {
			indicator.setAverageTick( avgTick );
			stockDayIndicatorDao.update( indicator );
		}
		
	}

	
	

	@Override
	public List<StockDayIndicator> findAllDayIndicator(String stockCode)
			throws ServiceException {
		// TODO Auto-generated method stub
		return stockDayIndicatorDao.findAllDayIndicator(stockCode);
	}


	@Override
	public void analyseAndUpdateStockDayIndicator(String stockCode, Date refDate)
			throws ServiceException {
		// TODO Auto-generated method stub
		List<StockTick> stockTickList = stockTickDao.findOneDayAllTicks(stockCode, refDate);
		StockDayIndicator indicator = stockDayIndicatorDao.getDayIndicator(stockCode, refDate);
		
		indicator = analysisTickHandler.analyseTicks(indicator, stockTickList);
		
		stockDayIndicatorDao.update( indicator );
		
	}


	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
	}

	
	
}
